package com.syh.bishe.controller;


import com.syh.bishe.Util.UserHolder;
import com.syh.bishe.mapper.CarbonCreditsMapper;
import com.syh.bishe.pojo.*;
import com.syh.bishe.service.IBillService;
import com.syh.bishe.service.ICarbonCreditsService;
import com.syh.bishe.service.ILogService;
import com.syh.bishe.service.ITransactionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author syh
 * @since 2024-07-02
 */
@Tag(name = "TransactionsController", description = "交易模块")
@RestController
@RequestMapping("/transactions")
@RequiredArgsConstructor
public class TransactionsController {
    private final ITransactionsService transactionsService;

    @PostMapping("/add")
    @Operation(summary = "发布交易")
    public Result add(@RequestBody Transactions transactions) {
        transactions.setSellerId(UserHolder.getUser().getCompanyId());
        return transactionsService.add(transactions);
    }

    @GetMapping("/getTransactions")
    @Operation(summary = "根据卖方获取交易列表")
    public Result getTransactions() {
        Integer sellerId = UserHolder.getUser().getCompanyId();
        return Result.success(transactionsService.getTransactions(sellerId));
    }

    @PutMapping("/purchase")
    @Operation(summary = "购买")
    @Transactional
    public Result purchase(@RequestBody Transactions transactions) {
        transactions.setBuyerId(UserHolder.getUser().getCompanyId());
        return transactionsService.purchase(transactions);
    }

    @PutMapping("/placeBid")
    @Operation(summary = "竞拍")
    @Transactional
    //使用悲观锁或者乐观锁
    public Result placeBid(@RequestBody Transactions transactions) {
        transactions.setBuyerId(UserHolder.getUser().getCompanyId());
        return transactionsService.placeBid(transactions);
    }

    @GetMapping("/getTransactionsById/{id}")
    @Operation(summary = "根据id获取交易的最高出价")
    public Result getTransactionsById(@PathVariable Integer id) {
        return Result.success(transactionsService.getById_transactions(id));
    }

    @GetMapping("/getTransactionsByBuyerId")
    @Operation(summary = "根据买方获取交易列表，状态为：卖家已确认", description = "卖家已确认，等待买家付款")
    public Result getTransactionsByBuyerId() {
        return Result.success(transactionsService.getTransactionsByBuyerId(UserHolder.getUser().getCompanyId()));
    }

    @GetMapping("/getTransactionsByBuyerId_a")
    @Operation(summary = "根据买方获取交易列表，状态为：卖家结束竞拍", description = "卖家结束竞拍,等待买家付款")
    public Result getTransactionsByBuyerId_a() {
        return Result.success(transactionsService.getTransactionsByBuyerId_a(UserHolder.getUser().getCompanyId()));
    }

    @PutMapping("/confirmTransaction/m3")
    @Operation(summary = "买家确认购买")
    @Transactional
    public Result confirmTransaction_m3(@RequestBody Transactions transactions) {
        return transactionsService.confirmTransaction_m3(transactions);
    }

    @PutMapping("/confirmTransaction/m4")
    @Operation(summary = "卖家确认出售")
    @Transactional
    public Result confirmTransaction_m4(@RequestBody Transactions transactions) {
        return transactionsService.confirmTransaction_m4(transactions);
    }

    @PutMapping("/confirmTransaction_a/m4")
    @Operation(summary = "卖家确认结束竞拍")
    @Transactional
    public Result confirmTransaction_a_m4(@RequestBody Transactions transactions) {
        return transactionsService.confirmTransaction_a_m4(transactions);
    }

    @GetMapping("/getTransactionsBySellerId")
    @Operation(summary = "根据卖方获取交易列表，状态为：已提交", description = "买家已提交订单申请，等待卖家确认")
    public Result getTransactionsBySellerId() {
        return Result.success(transactionsService.getTransactionsBySellerId(UserHolder.getUser().getCompanyId()));
    }

    @GetMapping("/getTransactionsBySellerId_a")
    @Operation(summary = "根据卖方获取交易列表，状态为：竞拍中", description = "等待卖家结束竞拍")
    public Result getTransactionsBySellerId_a() {
        return Result.success(transactionsService.getTransactionsBySellerId_a(UserHolder.getUser().getCompanyId()));
    }

    @GetMapping("/getTMR")
    @Operation(summary = "获取买方交易信息", description = "首页的数据获取：碳买入")
    public Result getTMR() {
        DataIndex dataIndex = transactionsService.getTMR(UserHolder.getUser().getCompanyId());
        if (dataIndex != null) {
            return Result.success(dataIndex);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getTMRvalue")
    @Operation(summary = "获取买方交易信息", description = "首页的数据获取：碳买入支出")
    public Result getTMRvalue() {
        DataIndex dataIndex = transactionsService.getTMRvalue(UserHolder.getUser().getCompanyId());
        if (dataIndex != null) {
            return Result.success(dataIndex);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getTMCvalue")
    @Operation(summary = "获取卖方交易信息", description = "首页的数据获取：碳卖出收入")
    public Result getTMCvalue() {
        DataIndex dataIndex = transactionsService.getTMCvalue(UserHolder.getUser().getCompanyId());
        if (dataIndex != null) {
            return Result.success(dataIndex);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getTransactionSell")
    @Operation(summary = "根据卖方获取列表", description = "公司碳交易平台页面的数据获取")
    public Result getTransactionSell() {
        List<Transactions> transactions = transactionsService.getTransactionSell(UserHolder.getUser().getCompanyId());
        if (transactions != null) {
            return Result.success(transactions);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getTransactionBuy")
    @Operation(summary = "根据买方获取列表", description = "公司碳交易平台页面的数据获取")
    public Result getTransactionBuy() {
        List<Transactions> transactions = transactionsService.getTransactionBuy(UserHolder.getUser().getCompanyId());
        if (transactions != null) {
            return Result.success(transactions);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getNotification")
    @Operation(summary = "获取通知", description = "通知信息的获取")
    public Result getNotification() {
        List<Notifications> notifications = transactionsService.getNotification(UserHolder.getUser().getCompanyId());
        if (notifications != null) {
            return Result.success(notifications);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getAllMainSome")
    @Operation(summary = "获取活动", description = "主页活动列表的获取")
    public Result getAllMainSome() {
        List<Transactions> notifications = transactionsService.getAllMainSome(UserHolder.getUser().getCompanyId());
        if (notifications != null && notifications.size() != 0) {
            return Result.success(notifications);
        } else {
            return Result.error("获取失败");
        }
    }

    //    -----------------admin进行碳交易的审核---------------
    @GetMapping("/getTransaction/admin")
    @Operation(summary = "获取所有交易", description = "管理员进行碳交易发布的审核")
    public Result getTransaction_admin() {
        List<Transactions> transactions = transactionsService.getList();
        if (transactions != null) {
            return Result.success(transactions);
        } else {
            return Result.error("获取失败");
        }
    }

    @GetMapping("/getTransaction/admin_f")
    @Operation(summary = "获取所有交易", description = "管理员进行碳交易成交的审核")
    public Result getTransaction_admin_f() {
        List<Transactions> transactions = transactionsService.getLists();
        if (transactions != null) {
            return Result.success(transactions);
        } else {
            return Result.error("获取失败");
        }
    }

    @PutMapping("/confirmTransaction/admin")
    @Operation(summary = "管理员审核", description = "管理员进行碳交易发布的审核")
    @Transactional
    public Result confirmTransaction_admin(@RequestBody Transactions transactions) {
        return transactionsService.confirmTransaction_admin(transactions);
    }

    @PutMapping("/confirmTransaction/admin_a")
    @Operation(summary = "管理员审核", description = "管理员进行碳交易成交的审核")
    @Transactional
    public Result confirmTransaction_admin_a(@RequestBody Transactions transactions) {
        return transactionsService.confirmTransaction_admin_a(transactions);
    }

    //    ---------android-----
    @GetMapping("getoneById")
    @Operation(summary = "根据id获取交易", description = "Android根据id获取交易")
    public Result getoneById(@RequestParam("id") Integer id) {
        Transactions transactions = transactionsService.getById(id);
        if (transactions != null) {
            return Result.success(transactions);
        } else {
            return Result.error("获取失败");
        }
    }
}
